package com.qzyh.esb.egsa.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qzyh.esb.egsa.bean.vo.MenuTreeVO;
import com.qzyh.esb.egsa.bean.vo.RoleVO;
import com.qzyh.esb.egsa.entity.Dept;
import com.qzyh.esb.egsa.entity.Role;
import com.qzyh.esb.egsa.entity.RoleMenu;
import com.qzyh.esb.egsa.mapper.RoleMapper;
import com.qzyh.esb.egsa.mapper.RoleMenuMapper;
import com.qzyh.esb.egsa.service.IRoleService;
import com.qzyh.esb.egsa.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.lang.reflect.InvocationTargetException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author whq
 * @since 2023-03-16
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private RoleMenuMapper roleMenuMapper;

    @Override
    public IPage<Role> getList(IPage<Role> page, String roleName) {
        QueryWrapper<Role> wrapper = Wrappers.query();
        if (StringUtils.hasLength(roleName)){
            wrapper.like("role_name", roleName);
        }
        wrapper.orderByAsc("role_sort+1");
        return roleMapper.selectPage(page, wrapper);
    }

    @Override
    @Transactional
    public ResultObject deleteById(String id) {
        QueryWrapper<RoleMenu> wrapper = Wrappers.query();
        wrapper.eq("role_id", id);
        roleMenuMapper.delete(wrapper);
        roleMapper.deleteById(id);
        return ResultObject.success("删除成功");
    }

    @Override
    @Transactional
    public ResultObject saveRoleAndMenu(RoleVO vo)  {
        Role role =  RoleConverter.INSTANCE.roleVOToRole(vo);
        List<String> menuList = vo.getMenuList();
        role = (Role) FillUtil.fill(role);
        if(StringUtils.hasLength(role.getId())){
            roleMapper.updateById(role);
            //修改角色信息时，先删除该角色关联的菜单信息，后续重新插入。
            QueryWrapper<RoleMenu> wrapper = Wrappers.query();
            wrapper.eq("role_id", role.getId());
            roleMenuMapper.delete(wrapper);
        } else {
            role.setId(UuidUtil.getId());
            roleMapper.insert(role);
        }
        //如果菜单集合不为空，则新增角色菜单表
        if (menuList != null && menuList.size() >0)
            insertRoleMenu(menuList,role.getId());
        return ResultObject.success("保存成功");
    }

    @Override
    public ResultObject<List<String>> getRoleAndMenu(String id) {
        QueryWrapper<RoleMenu> wrapper = Wrappers.query();
        wrapper.eq("role_id", id);
        List<RoleMenu> roleMenuList = roleMenuMapper.selectList(wrapper);
        List<String> list = new ArrayList<>();
        if (roleMenuList != null && roleMenuList.size() > 0){
            for (int i = 0; i < roleMenuList.size(); i++) {
                list.add(roleMenuList.get(i).getMenuId());
            }
        }
        return ResultObject.success(list);
    }

    @Override
    public List<Role> queryByIds(String idsStr) {
        if (StringUtils.hasLength(idsStr)){
            QueryWrapper<Role> wrapper = Wrappers.query();
            String[] idArray = idsStr.split(",");
            for (int i = 0; i < idArray.length; i++) {
                wrapper.eq("id", idArray[i]).or();
            }
            wrapper.orderByAsc("role_sort+1");
            return roleMapper.selectList(wrapper);
        }
        return null;
    }

    //插入角色菜单表
    private  void insertRoleMenu(List<String> menuList, String roleId){
        for (int i = 0; i < menuList.size(); i++) {
            RoleMenu roleMenu = new RoleMenu();
            roleMenu.setMenuId(menuList.get(i));
            roleMenu.setRoleId(roleId);
            //设置创建时间
            roleMenu.setCreateTime(LocalDateTime.now());
            //设置创建人员
            roleMenu.setCreateUserId(BaseContextUtil.getUserId());
            roleMenuMapper.insert(roleMenu);
        }
    }
}
